package com.ww.springboot.boot.old;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author wanwei
 * @TODO 豆鸡
 * @date: 2018年9月27日 下午4:42:54
 */
public class DoujiGame extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public DoujiGame() {
        add(new composition());
    }

    public static void main(String[] args) {
        JFrame frame = new DoujiGame();
        frame.setTitle("doujigame");
        frame.setSize(500, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static class composition extends JPanel {
//        private int originX = 50;
//        private int originY = 50;
        /**
         * 分支数
         */
        private int size = 10;
        private int[][] dataxx = new int[size + 1][size + 1];
        private int[] xs = new int[size];
        private int[] ys = new int[size];

        /**
         * 直径
         */
        private int diameter = 6;

        private boolean lock = false;
        /**
         * 存储球数
         */
        private int num = 20;

        private JButton jbt = new JButton("刷新");
        private JButton jbt2 = new JButton("大");
        private JButton jbt3 = new JButton("小");

        public composition() {

            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(1, 3, 1, 1));
            panel.add(jbt,0);
            panel.add(jbt2,1);
            panel.add(jbt3,2);
            add(panel, BorderLayout.SOUTH);
            jbt.addActionListener(new JbtListener());
            jbt2.addActionListener(new JbtListener2());
            jbt3.addActionListener(new JbtListener3());

            Timer timer = new Timer(10, new BallMoveListener());
            timer.start();
        }

        class JbtListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                dataxx = new int[size][size];
            }
        }

        class JbtListener2 implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                diameter +=2;
            }
        }

        class JbtListener3 implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(diameter == 2){
                    return;
                }
                diameter -=2;
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.BLACK);

            if (lock) {
                return;
            }
            lock = true;
            drawGame(g, 50, 50, 0);
            drawGame(g, 200, 50, 1);
            drawGame(g, 350, 50, 2);
            drawGame(g, 50, 300, 3);
            drawGame(g, 200, 300, 4);
            drawGame(g, 350, 300, 5);
            //drawGame(g,400,50,2);
            //g.drawOval(originX + 15, originY + 5, diameter, diameter);
            lock = false;
        }

        private void drawGame(Graphics g, int originX, int originY, int index) {
            if (xs[index] == 0) {
                xs[index] = originX;
                ys[index] = originY;
            }
            for (int i = 0; i < size; i++) {
                g.setColor(Color.BLACK);
                //左八线
                g.drawLine(originX - diameter * (i - 1), originY + diameter * 2 * i, originX - diameter * i, originY + diameter * 2 * (i + 1));
                //右八线
                g.drawLine(originX + diameter * 2 * 2 + diameter * (i - 1), originY + diameter * 2 * i, originX + diameter * 2 * 2 + diameter * i, originY + diameter * 2 * (i + 1));
                for (int j = 0; j < i + 1; j++) {
                    g.setColor(Color.blue);
                    g.drawOval(originX - diameter / 2 + diameter * 2 * (j + 1) - diameter * i, originY - diameter / 2 + diameter * 2 * (i + 1), diameter, diameter);
                    if (i == size - 1) {
                        g.setColor(Color.BLACK);
                        g.drawLine(originX + diameter * 2 * (j + 1) - diameter * i, originY + diameter * 2 * (i + 1) + diameter / 2, originX + diameter * 2 * (j + 1) - diameter * i, originY + diameter * 2 * (i + 1) + diameter / 2 + diameter * num);
                    }
                }
            }
            g.setColor(Color.BLACK);
            g.drawLine(originX - diameter * 2 * (size / 2 - 1) - diameter, originY + diameter * 2 * size, originX - diameter * 2 * (size / 2 - 1) - diameter, originY + diameter * 2 * size + diameter * num + diameter / 2);
            g.drawLine(originX + diameter * 2 * (size / 2 + 1) + diameter, originY + diameter * 2 * size, originX + diameter * 2 * (size / 2 + 1) + diameter, originY + diameter * 2 * size + diameter * num + diameter / 2);
            g.drawLine(originX - diameter * 2 * (size / 2 - 1) - diameter, originY + diameter * 2 * size + diameter * num + diameter / 2, originX + diameter * 2 * (size / 2 + 1) + diameter, originY + diameter * 2 * size + diameter * num + diameter / 2);
            g.drawLine(originX + diameter, originY, originX + diameter, originY - diameter * 5);
            g.drawLine(originX + diameter * 3, originY, originX + diameter * 3, originY - diameter * 5);
            for (int i = 0; i < dataxx[index].length; i++) {
                for (int j = 0; j < dataxx[index][i]; j++) {
                    g.setColor(Color.RED);
                    g.drawOval(originX + diameter * 2 * (i - (size/2 -1)) - diameter / 2, originY + diameter * 2 * size + num * diameter - j * diameter - diameter / 2, diameter, diameter);
                }

            }
            for (int i = 0; i < dataxx[index].length; i++) {
                if (dataxx[index][i] > 20) {
                    String s = i + 1 + "";
                    g.drawString(s, originX + diameter * 2 * i - (diameter * 2 * 4) - diameter / 2, originY + diameter * 2 * (size + 1) + diameter / 2 + diameter * num);
                    return;
                }
            }


            int a = (int) (Math.random() * 2);
            if (ys[index] < originY + (diameter * 2 * size)) {
                if (a == 0) {
                    xs[index] -= diameter;
                } else {
                    xs[index] += diameter;
                }
                ys[index] += diameter * 2;
            } else {
                if (ys[index] < originY + (diameter * 2 * size) + (num - 2) * diameter) {
                    ys[index] += diameter * 2;
                } else {
                    int sort = (xs[index] - (originX - diameter * (size + 1))) / (diameter * 2);
                    dataxx[index][sort]++;

                    xs[index] = originX;
                    ys[index] = originY;
                }
            }

            g.setColor(Color.RED);
            g.drawOval(xs[index] + diameter * 2 - diameter / 2, ys[index] + diameter / 2, diameter, diameter);
        }

        class BallMoveListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        }
    }
}


